<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ConsultationRecord;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ConsultationRecordsController extends Controller {


    //列表
    public function index(Request $request){
        $searchData = $request->post();

        $model = ConsultationRecord::from('consultation_records as r')
            ->leftJoin('users as u', 'u.id', '=', 'r.asker_id')
            ->leftJoin('consultations as c', 'c.id', '=', 'r.consultation_id')
            ->select(['r.*', 'u.nickname', 'c.describe'])
            ->orderBy('r.id', 'DESC');

        if ($consultation_id = $searchData['consultation_id'] ?? '') {
            $model->where('consultation_id', $consultation_id);
        }
//        if ($describe = $searchData['describe'] ?? '') {
//            $model->where('describe', 'like', "%{$describe}%");
//        }

        $result = $model->paginate(config('app.limit'));

        return $this->writeJson(0, [
            'data' => $result->items(),
            'count' => $result->total()
        ]);
    }


    //删除
    public function destroy(Request $request){
        $id = (int)$request->post('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        ConsultationRecord::destroy($id);

        return $this->writeJson(0, null, '删除成功');
    }

}
